discard
	
set more off 

/** naive lifetime prevalence and onsets **/

	use "$healthsave\basic healthvars_w7.dta", clear 
	
	** differencing lifetime prevalence is straightforward. 
	** getting naive onsets of cancer/heart attacks/strokes 
	**    is less straightforward 
	
	** create 'first' onset variables here: 
	
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			forv i = 0/7 { 
			
			gen new`x'_w`i' = .
			lab var new`x'_w`i' "Diagnosed with `1' since previous wave" 
			
			}
		mac shift 
		}
		
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 		
		forv i = 2/7 { 
			local Pi = `i'-1 
			
			replace new`x'_w`i' = 1 ///
				if ever`x'_w`i' == 1 & ever`x'_w`Pi' == 0 
			
			replace new`x'_w`i' = 0 ///
				if ever`x'_w`i' == 1 & ever`x'_w`Pi' == 1 
				
			replace new`x'_w`i' = 0 ///
				if ever`x'_w`i' == 0 
				
			replace new`x'_w`i' = -1 /// 
				if alive_w`i' == 1 & ///
				(ever`x'_w`i' == -1  | ever`x'_w`Pi' == -1) 
				
			replace new`x'_w`i' = -2 if alive_w`Pi' !=1 & alive_w`i' == 1 			
				
		} 
		}
		
		forv i = 1/7 { 
			gen onsetca_w`i' = newca_w`i'  
			gen onsetmi_w`i' = newmi_w`i' 
			gen onsetst_w`i' = newst_w`i' 
		}
	
foreach x in an mi hf di st ar ca pd ps ad de bl { 
		forv i = 1/7 { 
		format yrd`x'_w`i' %ty 
		*format mnthd`x'_w`i' %tm 
		gen onsetdate`x'`i' = ym(yrd`x'_w`i' , mnthd`x'_w`i') if yrd`x'_w`i' >0 & mnthd`x'_w`i'>0  
		format onsetdate`x'`i' %tm 
		}
	}
		
		** sometimes month is missing. 
		** if mnth is missing and reported year of diagnosis is equal to 
		** a previous interview wave, set month equal to month 
		** that intvw + 1 
		
		** if not, set month equal to jan 

	
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
		forv i = 2/7 { 
		local Pi = `i'-1 
			replace onsetdate`x'`i' = ym(yrd`x'_w`i' , 1) ///
				if (yrd`x'_w`i' != intyear_w1 & yrd`x'_w`i' != intyear_w2 & yrd`x'_w`i' != intyear_w3 & yrd`x'_w`i' != intyear_w4 & yrd`x'_w`i' != intyear_w5 & yrd`x'_w`i' != intyear_w6) ///
				 & yrd`x'_w`i' >0 & yrd`x'_w`i' !=. ///	/* yr of diagnosis non-missing */
				 & mnthd`x'_w`i' <=0 		/* month of diagnosis missing */
		
			forv w = 1/6 { 
			replace onsetdate`x'`i' = ym(yrd`x'_w`i' , intmonth_w`w' +1 ) ///
				if intmonth_w`w'!=12 ///
				& (yrd`x'_w`i' == intyear_w`w') ///
				 & yrd`x'_w`i' >0 & yrd`x'_w`i' !=. ///	/* yr of diagnosis non-missing */
				 & mnthd`x'_w`i' <=0 		/* month of diagnosis missing */
			
			replace onsetdate`x'`i' = ym(yrd`x'_w`i' +1 , 1 ) ///
				if intmonth_w`w'==12 ///
				& (yrd`x'_w`i' == intyear_w`w') ///
				 & yrd`x'_w`i' >0 & yrd`x'_w`i' !=. ///	/* yr of diagnosis non-missing */
				 & mnthd`x'_w`i' <=0 		/* month of diagnosis missing */
					
			}		
		}
	}

	
	** use reported AGE of onset to fill in onset dates 
	
	** first work out which wave is the LAST wave that respondent 
	** is <= reported age of diagnosis. 
	
	** onset date is == that wave if CONTEMPORANEOUS report 
	** onset date is == that wave + 1 month if LATER report 
	
	
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
		forv w = 1/7 { 		/* wave of report */
		
			gen lastwave_notd`x'_w`w' = . 	
		
		forv i = 1/7 { 		/* wave to be referenced */
			if `i' < `w' {
			replace lastwave_notd`x'_w`w' = `i' if agew`i' <= aged`x'_w`w'  & agew`i'!=. & aged`x'_w`w'!=. 
			}
			
			if `i' == `w' { 
			replace lastwave_notd`x'_w`w' = `i'-1 if agew`i' == aged`x'_w`w' & agew`i'!=. 
			}	
		}		
		replace lastwave_notd`x'_w`w' = firstw if lastwave_notd`x'_w`w' ==. & aged`x'_w`w' >0 & aged`x'_w`w'!=. 
		}	
	}

		
	foreach x in an mi hf di st ar ca pd ps ad de bl { 
	forv i = 1/7 { 
	forv w = 1/7 { 
		replace onsetdate`x'`i' = ym(intyear_w`w' , intmonth_w`w' +1) ///
				if lastwave_notd`x'_w`i' == `w' &  intmonth_w`w'!=12 
		
		replace onsetdate`x'`i' = ym(intyear_w`w' + 1 , 1) ///
				if lastwave_notd`x'_w`i' == `w' &  intmonth_w`w'==12 
	} 
	}
	}
	
			
		** now fill in 'naive' heart attacks and strokes. 
				
		forv i = 2/7 { 
			local Pi = `i' - 1 
			
			replace onsetmi_w`i' = 1 if henmmiw`i' >=1 & henmmiw`i' <=3 ///
						& evermi_w`i' == 1 & inwave`Pi'!=0 
			
			replace onsetst_w`i' = 1 if henmstw`i' >=1 & henmstw`i' <=3 ///
						& everst_w`i' == 1  & inwave`Pi'!=0 
				
				
		** fill in info on cancer (naive onset date) 		
			replace onsetca_w`i' = 1 	///
						if onsetdateca`i' < intdate_w`i' ///
						& onsetdateca`i' >= intdate_w`Pi' ///
						& everca_w`i' == 1 ///
						& alive_w`i' == 1 		
		}
		
		
		preserve 
		keep idauniq ever??_w? onset??_w? new??_w? 
		drop *w0 
		
		save "$healthsave\naive health variables wide_with_W7.dta", replace 
